"""
@author: dysb
@file: 21.剪刀石头布.py
@time: 2024/8/23 14:39
"""

# 可以创建游戏对象
# 游戏对象可以加入到房间
# 同一个房间内的两个游戏对象可以进行游戏，一个房间最多两个人
# 每次两个游戏对象随机的出石头剪刀布
# 三局两胜

# 玩家
# 属性 姓名、房间
# 方法 战斗

# 房间
# 属性 房间号，房间里的成员
# 方法 加入玩家
import random
class Player:
    def __init__(self,name,room_id = None):
        self.name = name
        self.room_id = room_id
        self.blood = 2
        print(f"玩家{self.name}创建成功!")
    def fight(self,player2):
        while True:
            if self.get_choice() == player2.get_choice():
                print(f"平局!")
            elif self.get_choice() == ((player2.get_choice() + 1) % 2):
                print(f"{self.name}获胜!")
                player2.blood -= 1
            else:
                print(f"{player2.name}获胜!")
                self.blood -=1

            if self.blood == 0:
                print(f"玩家{player2.name}取得最终胜利!")
                break
            elif player2.blood == 0:
                print(f"玩家{self.name}取得最终胜利!")
                break

    def get_choice(self):
        return random.randint(0, 2)
    def show_player(self):
        print(f"玩家名:{self.name} 玩家房间ID:{self.room_id}")
class Room:
    def __init__(self,room_id):
        self.room_id = room_id
        self.room_members = []
        self.room_members_number = 0
        print(f"房间{room_id}创建成功!")
    def add_player(self,player):
        if player.room_id != None:
            print("玩家已经加入房间")
        elif self.room_members_number < 3:
            print(f"玩家{player.name}加入房间{self.room_id}")
            self.room_members.append(player)
            self.room_members_number +=1
            player.room_id = self.room_id
        else:
            print("房间已满!")
    def fight(self):
        if self.room_members_number >= 2:
            self.room_members[0].fight(self.room_members[1])
        else:
            print("房间玩家未满，无法开始!")
    def show_room(self):
        if not self.room_members:
            print(f"房间ID:{self.room_id} 房间内没有玩家")
        elif len(self.room_members) == 1:
            print(f"房间ID:{self.room_id} 房间人数: 1/2 房间玩家: {self.room_members[0].name}")
        else:
            print(f"房间ID:{self.room_id} 房间人数: {self.room_members_number}/2 房间玩家: {self.room_members[0].name} {self.room_members[1].name}")

print("欢迎来玩石头剪刀布")
print("0.查看选择\n1.创建房间\n2.查看所有房间\n3.创建玩家\n4.查看所有玩家\n5.将玩家加入房间\n6.选择房间战斗\n7.退出")
room_list = {}
player_list = {}
while True:
    choice = input("请输入你的选择:")
    if choice == "0":
        print("0.查看选择\n1.创建房间\n2.查看所有房间\n3.创建玩家\n4.查看所有玩家\n5.将玩家加入房间\n6.选择房间战斗\n7.退出")
    elif choice == "1":
        room_id = input("请输入房间ID:")
        if room_id == '':
            print("****请输入房间ID!****")
            continue
        if room_list.get(room_id,0) == 0:
            room = Room(room_id)
            room_list[room_id] = room
        else:
            print(f"房间ID:{room_id}已存在！")
    elif choice == "2":
        print("所有房间如下:")
        for k,v in room_list.items():
            v.show_room()
    elif choice == "3":
        player_name = input("请输入玩家名:")
        if player_name == '':
            print("****请输入玩家名!****")
            continue
        if player_list.get(player_name, 0) == 0:
            play = Player(player_name)
            player_list[player_name] = play
        else:
            print(f"玩家:{player_name}已存在！")
            print()
    elif choice == "4":
        print("所有玩家如下:")
        for k,v in player_list.items():
            v.show_player()
    elif choice == "5":
        play_name = input("请输入要加入房间的玩家名:")
        if play_name == '':
            print("****请输入玩家名!****")
            continue
        if player_list.get(play_name, 0) == 0:
            print(f"玩家{play_name}不存在!")
        else:
            add_to_room_id = input("请输入要加入的房间ID:")
            if add_to_room_id == '':
                print("****请输入房间ID!****")
                continue
            if room_list.get(add_to_room_id, 0) == 0:
                print(f"房间ID:{add_to_room_id}不存在!")
            else:
                room_list[add_to_room_id].add_player(player_list[play_name])
    elif choice == "6":
        fight_room_id = input("请输入要战斗的房间ID:")
        if fight_room_id == '':
            print("****请输入房间ID!****")
            continue
        if room_list.get(fight_room_id, 0) == 0:
            print(f"房间ID:{fight_room_id}不存在!")
        else:
            room_list[fight_room_id].fight()
    elif choice == "7":
        print("退出成功!")
        break
    else:
        print("请输入1-7!")

